#断言封装工具
import json
import os

import pymysql
from bs4 import BeautifulSoup

from config import DIR_PATH

#断言工具
def common_assert(response,status_code=None,expect_json=None,expect_text=None):
    #如果status_code不为空，那么就断言
    if status_code:
        assert response.status_code == status_code,f"预期状态码:{status_code},实际状态码:{response.status_code}"
    if expect_json:
        assert expect_json in response.json().get("description"),f'预期值:{expect_json},实际值:{response.json().get("description")}'
    if expect_text:
        assert expect_text in response.text,f'预期值:{expect_text},实际值:{response.text}'

#读取数据工具
def read_json(filename,key):
    filepath=os.path.join(DIR_PATH,"data",filename)
    arrs=[]
    with open(filepath,"r",encoding="utf-8")as f:
        for data in json.load(f).get(key):
            arrs.append(tuple(data.values())[1:])
        return arrs

#提取html
def html_parser(result):
    #1、提取html
    html=result.json().get("description").get("form")
    #2、获取bs对象
    bs=BeautifulSoup(html,"html.parser")
    #3、提取url
    url=bs.form.get("action")
    print(f"提取的三方url：{url}")
    #4、提取所有input数据
    data={}
    for input in bs.find_all("input"):
        data[input.get("name")]=input.get("value")
    return url,data

#连接数据库工具
def connect_mysql(sql):
    conn=None
    cursor=None
    try:
        #获取连接对象
        conn=pymysql.connect(host="121.43.169.97",user="student",password="P2P_student_2023",database="czbk_member",port=3306,charset="utf8",autocommit=True)
        #获取游标对象
        cursor=conn.cursor()
        #执行sql语句
        cursor.execute(sql)
        if sql.lower().split()[0]=="select":
            #返回所有数据
            return cursor.fetchall()
        else:
            return cursor.rowcount
    except Exception as e:
        print("出错了",e)
        #回滚
        conn.rollback()
    finally:
        if cursor:
            cursor.close()
        if conn:
            conn.close()

#清除数据方法
def clear_data():
    sql_1='DELETE info.* from mb_member as mb inner join mb_member_info as info on info.member_id=mb.id where mb.phone in ("15600001111","15600001127","15600001113","15612345560","15612345678","15612345670")'
    connect_mysql(sql_1)
    sql_2='DELETE login.* FROM mb_member AS mb INNER JOIN mb_member_login_log AS login ON login.member_id=mb.id WHERE mb.phone IN ("15600001111","15600001127","15600001113","15612345560","15612345678","15612345670")'
    connect_mysql(sql_2)
    sql_3='DELETE FROM mb_member WHERE phone IN ("15600001111","15600001127","15600001113","15612345560","15612345678","15612345670")'
    connect_mysql(sql_3)
    sql_4='DELETE FROM mb_member_register_log WHERE phone IN ("15600001111","15600001127","15600001113","15612345560","15612345678","15612345670")'
    connect_mysql(sql_4)


if __name__ == '__main__':
    sql_1='DELETE info.* from mb_member as mb inner join mb_member_info as info on info.member_id=mb.id where mb.phone in ("15600001111","15600001127","15600001113","15612345560","15612345678","15612345670")'
    print(connect_mysql(sql_1))